﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using OnlineRailway.EF;
using OnlineRailway.Administrator;

namespace OnlineRailway.UI.Administrator
{
    public partial class ScheduleUpdate : System.Web.UI.Page
    {
        private SqlCommand cmd;
        private SqlDataAdapter dataAdapter;
        private DataSet ds = new DataSet();
        private int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Convert.ToInt32(Request.QueryString["id"]);
            lblMes.Text = "";
            if (!IsPostBack)
            {
                BindSchedule();
            }
        }
        void BindSchedule()
        {
            try
            {
                if (Connect.Conn.State == ConnectionState.Closed)
                {
                    Connect.Conn.Open();
                }
                cmd = new SqlCommand("SELECT [StationName] FROM Stations", Connect.Conn);
                dataAdapter = new SqlDataAdapter(cmd);
                dataAdapter.Fill(ds);
                ddlStartStation.DataSource = ds;
                ddlStartStation.DataTextField = "StationName";
                ddlStartStation.DataValueField = "StationName";
                ddlStartStation.DataBind();
                ddlEndStation.DataSource = ds;
                ddlEndStation.DataTextField = "StationName";
                ddlEndStation.DataValueField = "StationName";
                ddlEndStation.DataBind();
            }
            catch (Exception ex)
            {
                lblMes.Text = "Error: " + ex.Message;
            }
            finally
            {
                Connect.Conn.Close();
            }
            //************************************************************************//
           
            using (var context = new RailwayOnlineEntities())
            {
                var obj = context.TrainSchedules.First(x => x.ScheduleID == id);
                ScheduleName = obj.ScheduleName;
                BetweenStation = obj.BetweenStation;
                Distances = obj.Distances;
                LongTime = obj.LongTime;
                Fare = obj.Fare;
                Description = obj.Description;
                ddlStartStation.SelectedValue = obj.StartStation;
                ddlEndStation.SelectedValue = obj.EndStation;
                ddlStatus.SelectedValue = obj.Status;

            }
        }

        protected String ScheduleName { get; set; }
        protected String BetweenStation { get; set; }
        protected String Distances { get; set; }
        protected Int32 LongTime { get; set; }
        protected Int32 Fare { get; set; }
        protected String Description { get; set; }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            var name = Request.Form["txtScheduleName"];
            var start = ddlStartStation.Text;
            var end = ddlEndStation.Text;
            var between = Request.Form["txtBetweenStation"];
            var distances = Request.Form["txtDistances"];
            var longtime = Convert.ToInt32(Request.Form["txtLongTime"]);
            var fare = Convert.ToInt32(Request.Form["txtFare"]);
            var des = Request.Form["txtaDes"];
            var status = ddlStatus.Text;
            
            if (name!="" && distances!="" && longtime.ToString()!="" && fare.ToString()!="")
            {
                using (var conext = new RailwayOnlineEntities())
                {
                    var obj=conext.TrainSchedules.First(x => x.ScheduleID == id);
                    if (obj!=null)
                    {
                        obj.ScheduleName = name;
                        obj.StartStation = start;
                        obj.EndStation = end;
                        obj.BetweenStation = between;
                        obj.Distances = distances;
                        obj.LongTime = longtime;
                        obj.Fare = fare;
                        obj.Description = des;
                        obj.Status = status;
                        conext.SaveChanges();
                        lblMes.Text = "Updated!";
                    }
                }
            }
        }
        

    }
}